package com.xuqm.sdhbwfu.app.ui

import android.Manifest
import android.content.Intent
import android.os.Bundle
import com.bigkoo.alertview.AlertView
import com.megvii.livenesslib.LivenessActivity
import com.xuqm.base.common.AppManager
import com.xuqm.base.common.ToolsHelper
import com.xuqm.base.extensions.loge
import com.xuqm.base.extensions.runWithPermission
import com.xuqm.base.extensions.showMessage
import com.xuqm.base.ui.BaseActivity
import com.xuqm.sdhbwfu.app.R
import com.xuqm.sdhbwfu.app.databinding.ActivityLoginBinding

class LoginActivity : BaseActivity<ActivityLoginBinding>() {
    override fun getLayoutId() = R.layout.activity_login
    override fun fullscreen() = true

    private val PAGE_INTO_LIVENESS = 100

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        binding.loginFace.setOnClickListener {
            val param = "<param>" +
                    "<imgWidth>640</imgWidth>" +
                    "<imgHeight>480</imgHeight>" +
                    "<imgCompress>85</imgCompress>" +
                    "<pupilDistMax>200</pupilDistMax>" +
                    "<pupilDistMin>70</pupilDistMin>" +
                    "<isActived>2</isActived>" +
                    "<timeOut>45</timeOut>" +
                    "<isAudio>1</isAudio>" +
                    "<version>1.1.3</version>" +
                    "<deviceIdx>0</deviceIdx>" +
                    "<cryptType>3</cryptType>" +  // 0:不加密    3:加密    不传或其他值默认为3
                    "<definitionAsk>15</definitionAsk>" +
                    "<headLeft>-12</headLeft>" +
                    "<headRight>15</headRight>" +
                    "<headLow>-8</headLow>" +
                    "<headHigh>10</headHigh>" +
                    "<mouthDegree>15</mouthDegree>" +
                    "<eyeDegree>25</eyeDegree>" +
                    "<interfaceType>2</interfaceType>" +
                    "<action>3</action>" +  // 动作个数： 0:静默采集 最多传3  不传默认为3
                    "<actionlist>1234</actionlist>" +
                    "</param>"

            runWithPermission(
                Manifest.permission.CAMERA,
                rationaleMethod = {
                    AlertView(
                        "无法获取摄像头使用权限", "人脸识别功能需要开启摄像头权限才能正常使用。",
                        null, arrayOf("确定"), null, mContext,
                        AlertView.Style.Alert
                    ) { _, _ ->
                    }.show()
                },
                permanentDeniedMethod = {
                    "请在设置界面打开摄像头权限".showMessage()
                    AlertView(
                        "无法获取摄像头使用权限", "人脸识别功能需要开启摄像头权限才能正常使用，请在应用权限管理页面打开相关权限。",
                        null, arrayOf("确定"), null, mContext,
                        AlertView.Style.Alert
                    ) { _, _ ->
                    }.show()
                },
                callback = {
                    startActivityForResult(
                        Intent(
                            mContext,
                            LivenessActivity::class.java
                        ).apply {

                            putExtra("param", param)
                            putExtra("serilarNumber", "12345678123456781234567812345678") // 流水号

                            putExtra("mute", true) // 是否开启语音，true静音

                            putExtra("isVertical", true) // 是否开启垂直，true：垂直，false：水平

                            putExtra(
                                "setSilent",
                                false
                            ) // true:不管xml配置是否做动作都变为静默加密模式, fasle:根据xml配置来做相应动作及加密方式。
                        }, PAGE_INTO_LIVENESS
                    )
                })

        }
        binding.loginPsw.setOnClickListener {
            startActivity(Intent(mContext, LoginPswActivity::class.java))
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode==PAGE_INTO_LIVENESS && resultCode == RESULT_OK){
            val result = data!!.getStringExtra("result")
            result?.loge()
        }
    }


    private var oldTime = 0L
    override fun onBackPressed() {
        val newTime = System.currentTimeMillis()
        if (newTime - oldTime < 1500 && oldTime != 0L)
            AppManager.getInstance().exit()
        else {
            oldTime = newTime
            ToolsHelper.showMessage("双击退出")
        }
    }
}